package com.managermype.business;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.springframework.stereotype.Service;

import com.managermype.util.Constant;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Service
public class XmlReaderBusiness {

	public Object objectReaderXml(Object obj, String xmlPath) {
		XStream xs = new XStream(new DomDriver(Constant.XML_ENCODING));
		xs.autodetectAnnotations(Boolean.TRUE);
		xs.processAnnotations(obj.getClass());
		try {
			obj = xs.fromXML(new FileInputStream(xmlPath));
			return obj;
		}
		catch (FileNotFoundException e) {
			System.err.println("No se encontró el archivo xml en la ruta :" + xmlPath);
		}
		return null;

	}

	public Object objectReaderXml(Object obj, InputStream is) {
		XStream xs = new XStream(new DomDriver(Constant.XML_ENCODING));
		xs.autodetectAnnotations(Boolean.TRUE);
		xs.processAnnotations(obj.getClass());
		try {
			obj = xs.fromXML(is);
			return obj;
		}
		catch (Exception e) {
			System.err.println("Hubo un problema con el archivo :");
		}
		return null;
	}

}
